package myky.com.doctorapp.base;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

import myky.com.doctorapp.utils.Constants;
import myky.com.doctorapp.utils.SecretSharePrefence;
import myky.com.doctorapp.utils.ShareUtil;
import myky.com.doctorapp.wight.CustomFooter;
import myky.com.doctorapp.wight.CustomHeader;


/**
 * Created by xiangyao on 2018/8/1.
 */

public class CMApplication extends MultiDexApplication {
    private static CMApplication demoApplication;
    private boolean isLoginOk = false;

    public static boolean showLog = true;


    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            return new CustomHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new CustomFooter(context);
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initFresco();
        demoApplication = this;
        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        UMConfigure.init(this, "597aaa8c45297d254a000147", "Cmai", UMConfigure.DEVICE_TYPE_PHONE,
                "be87da56c8bc87ac2932e101435904d1");

        PlatformConfig.setWeixin("wx068ff2d7221933e0", "e1d871c9c5ba1b937dda39bc857eece9");
        PlatformConfig.setQQZone("1106338457", "pbJb9OzNMBXcbns3");
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(this).setShareConfig(config);
        boolean state = ShareUtil.getBoolean(Constants.ISLOGIN);

        setLoginOk(state);

        UMConfigure.setLogEnabled(true);
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)
                .methodCount(0)
                .methodOffset(7)
                .tag("LOGINFO")
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));


        SecretSharePrefence.initSecreSharePrefence(this);
    }

    public static synchronized CMApplication get() {
        return demoApplication;
    }

    /**
     * 初始化fresco
     */
    private void initFresco() {
        Fresco.initialize(this);
    }

    public static Context getAppContext() {
        return demoApplication;
    }

    private static CMApplication instance = null;


    public static CMApplication getInstance() {
        synchronized (CMApplication.class) {
            if (instance == null) {
                instance = new CMApplication();
            }
        }
        return instance;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        //清空内存缓存（包括Bitmap缓存和未解码图片的缓存）
        imagePipeline.clearMemoryCaches();
    }

    public boolean isLoginOk() {
        boolean state = ShareUtil.getBoolean(Constants.ISLOGIN);

        return state;
    }

    public void setLoginOk(boolean loginOk) {
        this.isLoginOk = loginOk;
    }


}
